package cn.tedu._04weibo.controller;
import cn.tedu._04weibo.pojo.dto.WeiBoAddParam;
import cn.tedu._04weibo.base.response.JsonResult;
import cn.tedu._04weibo.base.response.StatusCode;
import cn.tedu._04weibo.mapper.WeiBoMapper;
import cn.tedu._04weibo.pojo.entity.WeiBo;
import cn.tedu._04weibo.pojo.vo.UserVO;
import cn.tedu._04weibo.pojo.vo.WeiBoDetailVO;
import cn.tedu._04weibo.pojo.vo.WeiBoIndexVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@Tag(name = "02-微博模块")
@Validated
@RestController
@RequestMapping("/v1/weibo/")
public class WeiBoController {
    @Autowired
    private WeiBoMapper weiBoMapper;

    /**
     * 1.发布微博功能
     */
    @PostMapping("insert")
    @Operation(summary = "1.发布微博功能")
    @ApiOperationSupport(order = 10)
    public JsonResult insert(@RequestBody WeiBoAddParam weiBoAddParam, HttpSession session) {
        log.debug("weiBoAddParam="+weiBoAddParam);
        //校验登录状态
        UserVO userVO = (UserVO) session.getAttribute("user");
        if(userVO == null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        WeiBo weiBo = new WeiBo();
        BeanUtils.copyProperties(weiBoAddParam, weiBo);
        weiBo.setCreateTime(new Date());
        weiBo.setUserId(userVO.getId());
        weiBoMapper.insertWeiBo(weiBo);
        return JsonResult.ok(weiBo);
    }

    /**
     * 2.微博首页列表
     */
    @GetMapping("selectIndex")
    @Operation(summary = "2.微博首页列表")
    @ApiOperationSupport(order = 20)
    public JsonResult selectIndex() {
        List<WeiBoIndexVO> weiBoVOList = weiBoMapper.selectIndex();
        return JsonResult.ok(weiBoVOList);
    }

    /**
     * 3.微博详情页
     */
    @GetMapping("selectById")
    @Operation(summary = "3.微博详情页")
    @ApiOperationSupport(order = 30)
    public JsonResult selectById(@Range(min = 100,max = 500,message = "微博ID必须在100-500之间")Long id) {
        log.debug("id="+id);
        WeiBoDetailVO weiBoDetailVO = weiBoMapper.selectById(id);
        return JsonResult.ok(weiBoDetailVO);
    }
}
